﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using TableParsers;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

using SpeakupBackend;
using SpeakupBackend.Themes;

namespace Frontend
{
    class UIElementFactory
    {
        private Border Wrap(UIElement control)
        {
            Border wrapper = new Border();
            wrapper.Child = control;
            wrapper.BorderThickness = new Thickness(0.75);

            return wrapper;
        }

        public UIElement CreateWeek(Week week)
        {
            return Wrap(OnCreateWeek(week));            
        }

        public UIElement CreateClassType(WorkshopClassType classType)
        {
            return Wrap(OnCreateClassType(classType));
        }

        public UIElement CreateLevelGroup(LevelGroup themeLevel)
        {
            return Wrap(OnCreateLevelGroup(themeLevel));
        }

        public UIElement CreateLevel(Level level)
        {
            return Wrap(OnCreateLevelGroup(level));
        }

        public Border CreateGroupThemeName(GroupThemeName name)
        {
            return Wrap(OnCreateGroupThemeName(name));
        }

        public UIElement CreateWorkshopTheme(WorkshopTheme weekTheme)
        {
            return Wrap(OnCreateWorkshopTheme(weekTheme));
        }

        public UIElement CreateGroupTheme(GroupTheme weekTheme)
        {
            return Wrap(OnCreateGroupTheme(weekTheme));
        }

        public UIElement CreateClubTheme(ClubTheme weekTheme)
        {
            return Wrap(OnCreateClubTheme(weekTheme));
        }  

        virtual protected UIElement OnCreateWeek(Week week)
        {
            TextBlock textBlock = new TextBlock();            
            textBlock.Text = String.Format("Week {0:00}",week.WeekNumber);
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;
            textBlock.Margin = new Thickness(3);
            return textBlock;
        }

        virtual protected UIElement OnCreateClassType(WorkshopClassType classType)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = classType.ToString();            
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;
            return textBlock;            
        }

        virtual protected UIElement OnCreateLevelGroup(LevelGroup themeLevel)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = String.Format("Level {0}", themeLevel.ToString());
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;
            return textBlock;
        }

        virtual protected UIElement OnCreateLevelGroup(Level level)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = String.Format("Level {0}", level.ToString());
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;
            return textBlock;
        }

        virtual protected UIElement OnCreateGroupThemeName(GroupThemeName name)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = name.ToString();
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;

            return textBlock;
        }

        virtual protected UIElement OnCreateWorkshopTheme   (WorkshopTheme weekTheme)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.TextWrapping = TextWrapping.Wrap;
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;

            Run textTheme = new Run();
            textTheme.Text = weekTheme.Name;
                        
            if (weekTheme.IsAttended)
            {
                textTheme.TextDecorations = TextDecorations.Strikethrough;
                textTheme.Foreground = new SolidColorBrush(Color.FromArgb(100, 20, 20, 20));
                textBlock.Inlines.Add(textTheme);
            }
            else
            {
                textBlock.Inlines.Add(textTheme);
            }

            return textBlock;
        }

        virtual protected UIElement OnCreateClubTheme(ClubTheme weekTheme)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.TextWrapping = TextWrapping.Wrap;
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;

            Run textTheme = new Run();
            textTheme.Text = weekTheme.Name;

            if (weekTheme.IsAttended)
            {
                textTheme.TextDecorations = TextDecorations.Strikethrough;
                textTheme.Foreground = new SolidColorBrush(Color.FromArgb(100, 20, 20, 20));
                textBlock.Inlines.Add(textTheme);
            }
            else
            {
                textBlock.Inlines.Add(textTheme);
            }

            return textBlock;
        }

        virtual protected UIElement OnCreateGroupTheme(GroupTheme theme)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.TextWrapping = TextWrapping.Wrap;
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;

            Run textTheme = new Run();
            

            if (theme.IsAttended)
            {
                textTheme.Text = "+";
            }
            else
            {
                textTheme.Text = "-";            
            }
            textBlock.Inlines.Add(textTheme);
            textBlock.FontSize = 20;

            Border border = new Border();
            border.Child = textBlock;

            if (theme.IsAttended)
            {
                border.Background = Brushes.LightBlue;
            }
            return border;
        }
    }
}
